package com.ahy231.controller;

import com.ahy231.dto.*;
import com.ahy231.service.*;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.*;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2021/8/6 16:35
 * @description
 */
@Api(tags = "交易执行")
@RestController
@RequestMapping("/task")
public class TaskController {

    private final TaskService taskService;

    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @ApiOperation("执行交易")
    @PostMapping(value = "/start", consumes = "application/x-www-form-urlencoded")
    @RequiresRoles(value = {"driver", "company"}, logical = Logical.OR)
    public Result start(@ApiParam("交易 id") @RequestParam Long transactionId) {
        try {
            return new Result(ResultCode.SUCCESS, taskService.startTask(transactionId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("申请验收")
    @PostMapping(value = "/verify", consumes = "application/x-www-form-urlencoded")
    @RequiresRoles(value = {"driver", "company"}, logical = Logical.OR)
    public Result verify(@ApiParam("交易 id") @RequestParam Long transactionId) {
        try {
            return new Result(ResultCode.SUCCESS, taskService.verifyTask(transactionId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("验收（true/通过、false/不通过）")
    @PostMapping(value = "/pass", consumes = "application/x-www-form-urlencoded")
    @RequiresRoles("worksite")
    public Result pass(@ApiParam("交易 id") @RequestParam Long transactionId, @ApiParam(value = "是否通过", required = true) @RequestParam Boolean pass) {
        try {
            return new Result(ResultCode.SUCCESS, taskService.pass(transactionId, pass));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
